package dao;

import bean.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import utils.JDBCUtils;

import java.sql.SQLException;
import java.util.List;

public class DaoRegisterlmpl implements DaoRegister {
    //    在dao层中完成对用户信息的查询
    public List register(User user) {
        QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
        String sql = "SELECT * FROM users WHERE username = ? and password = ?";
        List<User> s = null;
        try {
            s = queryRunner.query(sql, new BeanListHandler<User>(User.class),
                    user.getUserName(), user.getPassword());

            System.out.println(s);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  s;
    }
//    在dao层完成对用户信息的注册
    public int add(User user){
        QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
        int a =0;
        String sql = "INSERT  INTO users(username,password) VALUES (?,?);";
        try {
             a = queryRunner.update(sql,
                    user.getUserName(),user.getPassword());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return a;
    }
}
